import rhythmdb, rb
import md5
import time

from NetlogMusicConf import NetlogMusicConf
from NetlogCommunicator import NetlogCommunicator
from NetlogConfDialog import NetlogConfDialog

class NetlogMusicPlugin (rb.Plugin):
	def __init__(self):
		self.config = NetlogMusicConf()
		rb.Plugin.__init__(self)
		self.comm = NetlogCommunicator(self.config)
		
	def activate(self,shell):
		self.shell = shell
		player = shell.get_player()
		self.psc_id = player.connect('playing-song-changed',self.playing_song_changed)

		
	def deactivate(self,shell):
		del self.shell
		player = shell.get_player()
		player.disconnect(self.psc_id)
	
	def create_configure_dialog(self):
		gladef = self.find_file("nlmusic.glade")
		
		if gladef:
			dialog = NetlogConfDialog(gladef, self.config, self.comm).get_dialog()
		if dialog:
			return dialog
			
		print "Couln't create dialog"
		return None
	
	def playing_song_changed(self, player, entry):
		if not self.comm.isLoggedIn():
			return
			
		db = self.shell.get_property("db")
		location = db.entry_get (entry, rhythmdb.PROP_LOCATION)
		if not location.startswith("file:"):
			return					

		songInfo = {'method':'music.playSong',
					'sig':'',
					'ticket':'',
					'requestId':''}
					
		songInfo["album"] = db.entry_get (entry, rhythmdb.PROP_ALBUM) 
		songInfo["artist"] = db.entry_get (entry, rhythmdb.PROP_ARTIST) 
		songInfo["title"] = db.entry_get (entry, rhythmdb.PROP_TITLE) 
		songInfo["genre"] = db.entry_get (entry, rhythmdb.PROP_GENRE) 
		songInfo["year"] = str(db.entry_get (entry, rhythmdb.PROP_YEAR))
		songInfo["length"] = str(db.entry_get (entry, rhythmdb.PROP_DURATION))
#		songInfo["comment"] = db.entry_get (entry, rhythmdb.PROP_COMMENT) 
		songInfo["play_time"] = time.ctime()
		
#		print songInfo["title"] + " von " + songInfo["artist"] + " auf " + songInfo["album"] + " aus " + songInfo["year"] 
		self.comm.sendSong(songInfo)
		
